package com.xl.xlserver.gameServer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class GameServer {





    private static Logger log = LoggerFactory.getLogger(GameServer.class);



    public void start(int port) throws Exception {
        //配置服务端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new GameMsgHandler());
                        }
                    }).option(ChannelOption.SO_BACKLOG, 128) //最大客户端连接数为128
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            //绑定端口，同步等待成功
            ChannelFuture f = b.bind(port).sync();
            //等待服务端监听端口关闭
            f.channel().closeFuture().sync();
        } finally {
            //优雅退出，释放线程池资源
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }








//    private final int port;
//
//    public GameServer(int port) throws InterruptedException {
//        this.port = port;
//        initServer();
//    }
//
//    /**
//     * 日志对象
//     */
//    static private final Logger LOGGER = LoggerFactory.getLogger(GameServer.class);
//    public  void initServer() throws InterruptedException {
//        EventLoopGroup eventLoopGroup = null;
//        try {
//            //创建ServerBootstrap实例来引导绑定和启动服务器
//            ServerBootstrap serverBootstrap = new ServerBootstrap();
//            //创建NioEventLoopGroup对象来处理事件，如接受新连接、接收数据、写数据等等
//            eventLoopGroup = new NioEventLoopGroup();
//            //指定通道类型为NioServerSocketChannel，设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
//            serverBootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).localAddress("localhost", port).childHandler(new ChannelInitializer<Channel>() {
//                //设置childHandler执行所有的连接请求
//                @Override
//                protected void initChannel(Channel ch) throws Exception {
//                    ch.pipeline().addLast(new GameMsgHandler());
//                }
//            });
//            // 最后绑定服务器等待直到绑定完成，调用sync()方法会阻塞直到服务器完成绑定,然后服务器等待通道关闭，因为使用sync()，所以关闭操作也会被阻塞。
//            ChannelFuture channelFuture = serverBootstrap.bind().sync();
//            System.out.println("开始监听，端口为：" + channelFuture.channel().localAddress());
//            channelFuture.channel().closeFuture().sync();
//        } finally {
//            eventLoopGroup.shutdownGracefully().sync();
//        }
//    }
}
